﻿using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class OptionalRuleSpecification {
        [Test]
        public void ShouldParseNothing () {
            var opt = new OptionalRule (new KeywordRule ("x"));

            var source = "9";

            var result = opt.Parse (source, 0, new ParseContext ());
            Assert.IsNotNull (result);
            Assert.AreEqual (0, result.Index);
        }

        [Test]
        public void ShouldParseAtEndOfFile () {
            var opt = new OptionalRule (new KeywordRule ("x"));

            var source = "";

            var result = opt.Parse (source, 0, new ParseContext ());
            Assert.IsNotNull (result);
            Assert.AreEqual (0, result.Index);
        }

        [Test]
        public void ShouldParseSomething () {
            var opt = new OptionalRule (new KeywordRule ("x"));

            var source = "x";

            var result = opt.Parse (source, 0, new ParseContext ());
            Assert.IsNotNull (result);
            Assert.AreEqual (1, result.Index);
        }
    }
}